package com.markerhub.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.TWarnPhone;
import com.markerhub.service.ITWarnPhoneService;
import com.markerhub.vo.WarnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
* 预警监控表前端控制器
* @author zhz
* @since 2021-9-18 11:38:20
*/

@Slf4j
@Api(tags = "预警监控表", description = "预警监控表相关API")
@RestController
@RequestMapping("/warn")
public class TWarnPhoneController extends BaseController {

    @Autowired
    public ITWarnPhoneService tWarnPhoneService;

    /**
     * 新增
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody TWarnPhone tWarnPhone){
        tWarnPhoneService.save(tWarnPhone);
        return Result.succ("");
    }

    /**
     * 根据id删除
     */
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
        tWarnPhoneService.removeByIds(Arrays.asList(ids));
        return Result.succ("");
    }

    /**
     * 条件查询
     */
    @ApiOperation(value = "条件查询", notes = "条件查询")
    @PostMapping("/get")
    public Result list(@RequestBody TWarnPhone tWarnPhone){
        List<TWarnPhone> tWarnPhoneList = tWarnPhoneService.list(new QueryWrapper<>(tWarnPhone));
        return Result.succ(tWarnPhoneList);
    }

    /**
     * 列表(分页)
     */
    @ApiOperation(value = "列表(分页)", notes = "列表(分页)")
    @PostMapping("/list")
    public Result pageList(@RequestBody WarnVO warnVO){
        Page<TWarnPhone> page = new Page<>(warnVO.getCurrent(),warnVO.getSize());
        LambdaQueryWrapper<TWarnPhone> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(warnVO.getWarnName()),TWarnPhone::getWarnName,warnVO.getWarnName());
        wrapper.like(StringUtils.isNotBlank(warnVO.getWarnPhone()),TWarnPhone::getWarnPhone,warnVO.getWarnPhone());
        IPage<TWarnPhone> pageData = tWarnPhoneService.page(page, wrapper);
        return Result.succ(pageData);
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @GetMapping("/info/{id}")
    public Result get(@PathVariable("id") String id){
        TWarnPhone tWarnPhone = tWarnPhoneService.getById(id);
        return Result.succ(tWarnPhone);
    }

    /**
     * 根据id修改
     */
    @ApiOperation(value = "根据id修改", notes = "根据id修改")
    @PostMapping("/update")
    public Result update(@RequestBody TWarnPhone tWarnPhone){
        tWarnPhoneService.updateById(tWarnPhone);
        return Result.succ("");
    }

    /***
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/enable/{id}")
    public Result enable(@PathVariable("id") String id){
        tWarnPhoneService.enable(id);
        return Result.succ("");
    }

    /***
     * 禁用
     */
    @PostMapping("/disable/{id}")
    public Result disable(@PathVariable("id") String id){
        tWarnPhoneService.disable(id);
        return Result.succ("");
    }
}
